package com.example.pilipili.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pilipili.common.R;
import com.example.pilipili.pojo.dto.CommentDto;
import com.example.pilipili.pojo.entity.Comment;
import com.example.pilipili.service.CommentService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyv
 * @since 2023-12-01
 */
@RestController
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    CommentService commentService ;


    @PostMapping
    public R<Comment> add(@RequestBody @Valid Comment comment){
        return commentService.add(comment) ;
    }

    @GetMapping("/v/{vid}")
    public R<List<CommentDto>> gets(@PathVariable int vid) {
        return commentService.getsByVid(vid) ;
    }

    @GetMapping("/ban/{cid}")
    public R<Comment> ban(@PathVariable int cid) {
        return commentService.ban(cid) ;
    }

    @GetMapping("/del/{cid}")
    public R del(@PathVariable int cid) {
        return commentService.del(cid) ;
    }

    /**
     * 获取 未封禁的 帖子
     * @return list comment
     */
    @GetMapping("/s0")
    public R<List<Comment>> getsByStatus(){
        QueryWrapper<Comment> qw = new QueryWrapper<>() ;
        qw.eq("status",0);
        return R.ok(commentService.list(qw)) ;
    }

    /**
     * 根据 内容查询
     * @param content content
     * @return list Comment
     */
    @GetMapping("/content")
    public R<List<Comment>> getsByContent(@RequestParam("value")  String content){
        QueryWrapper<Comment> qw = new QueryWrapper<>() ;
        qw.eq("status",0).like("content",content);
        return R.ok(commentService.list(qw)) ;
    }

    /**
     * 根据 评论id查询
     * @param  id cid
     * @return Comment
     */
    @GetMapping("/i")
    public R<Comment> getsById(@RequestParam("value") int id){
        return R.ok(commentService.getById(id)) ;
    }



}

